package com.fix;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.nio.file.ReadOnlyFileSystemException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetImg {

	public static String readFile() throws Exception {
		StringBuffer sBuffer = new StringBuffer();
		File file = new File("lol.html");

		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String t = null;
		while ((t = br.readLine()) != null) {
			sBuffer.append(t);
		}
		br.close();
		fr.close();
		return sBuffer.toString();
	}

	// 正则分析图片
	// <img src="//ossweb-img.qq.com/images/lol/img/champion/Aatrox.png" alt="暗裔剑魔 亚托克斯">
	// 亚托克斯">
	public static List<String> regular(String info) {
		List list =new ArrayList();
		String reg="<img src=\"(.*?)\" alt=\"(.*?)\">";
		Pattern pattern = Pattern.compile(reg);
		Matcher mat = pattern.matcher(info);
		while (mat.find()) {
//			System.out.println(mat.group());
//			System.out.println(mat.group(1)+"--"+mat.group(2));
			list.add("https:"+mat.group(1));
		}
		return list;
	}
	
	 private static void download(List<String> listImgSrc) {
	        try {
	            //开始时间
	            Date begindate = new Date();
	            for (String url : listImgSrc) {
	                //开始时间
	                Date begindate2 = new Date();
	                String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());
	                URL uri = new URL(url);
	                InputStream in = uri.openStream();
	                FileOutputStream fo = new FileOutputStream(new File("lolhead/"+imageName));//文件输出流
	                byte[] buf = new byte[1024];
	                int length = 0;
	                System.out.println("开始下载:" + url);
	                while ((length = in.read(buf, 0, buf.length)) != -1) {
	                    fo.write(buf, 0, length);
	                }
	                //关闭流
	                in.close();
	                fo.close();
	                System.out.println(imageName + "下载完成");
	                //结束时间
	                Date overdate2 = new Date();
	                double time = overdate2.getTime() - begindate2.getTime();
	                System.out.println("耗时：" + time / 1000 + "s");
	            }
	            Date overdate = new Date();
	            double time = overdate.getTime() - begindate.getTime();
	            System.out.println("总耗时：" + time / 1000 + "s");
	        } catch (Exception e) {
	        	e.printStackTrace();
	            System.out.println("下载失败");
	        }
	    } 

	public static void main(String[] args) {
		try {
			String ok = readFile();
			List filelist= regular(ok);
//			System.out.println(filelist);
			download(filelist);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
